﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Operate
{
	int a;		 //4
	int b;		 //4
	char c;		 //1
	float value; //4    16
};
typedef struct Operate opt;

//main150：结构体实现文本文件读写四则运算表达式和运算结果
int main150()
{

	opt* p = (opt*)malloc(sizeof(opt) * 100);
	//1.r+是可以读写；注意：不能写成w+，否则直接出错；所有的地方，涉及到读写，都要写成r+。
	FILE* fp = fopen("../../../file/FileFourOperationResultConstruct.txt", "r+");
	if (!fp)
	{
		return -1;
	}
	for (int i = 0; i < 100; i++)
	{
		fscanf(fp, "%d%c%d=\n", &(p + i)->a, &p[i].c, &p[i].b);
		printf("%d%c%d=\n", p[i].a, p[i].c, p[i].b);//5+5=	...	 5+5=
		switch (p[i].c)
		{
		case '+':
			p[i].value = p[i].a + p[i].b;
			break;
		case '-':
			p[i].value = p[i].a - p[i].b;
			break;
		case '*':
			p[i].value = p[i].a * p[i].b;
			break;
		case '/':
			p[i].value = p[i].a * 1.0 / p[i].b;
			break;
		}
	}
	fclose(fp);
	fp = fopen("../../../file/FileFourOperationResultConstruct.txt", "r+");
	if (!fp)
	{
		return -1;
	}
	for (int i = 0; i < 100; i++)
	{
		fprintf(fp, "%d%c%d=%.2f\n", p[i].a, p[i].c, p[i].b, p[i].value);
	}
	fclose(fp);
	free(p);

	system("pause");
	return EXIT_SUCCESS;
}